package com.l.im.service.init;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;

/**
 * @program: LIM
 * @description: zk注册
 * @author: Alex Wu
 * @createDate: 2025-04-14 17:22
 **/
@Slf4j
public class WebSocketServiceRegistry {

    private final CuratorFramework client;
    private final String zkPath;

    public WebSocketServiceRegistry(CuratorFramework client, String ip, int port) {
        this.client = client;
        this.zkPath = "/ws/nodes/" + ip + ":" + port;
    }

    public void register() throws Exception {
        if (client.checkExists().forPath(zkPath) == null) {
            client.create()
                    .creatingParentsIfNeeded()
                    .withMode(CreateMode.EPHEMERAL)
                    .forPath(zkPath, "WebSocketNode".getBytes());

            log.info("成功注册到zookeeper节点:{}", zkPath);
        }
    }
}

